### dan hopkins
### 12/11/2020
### aca panel data

#### libraries ----
library(texreg)

the_prefix <- ""

load(file="data/table2-panel-aca-replication-12112020.Rdata")

lout1 <- lm(I(ACASCALE13-ACASCALE11) ~ I(UNINSURED13-UNINSURED11)+I(SELFINSURE13-SELFINSURE11)+
             I(MEDICAID13-MEDICAID11)+
             as.factor(PID11)+as.factor(ACASCALE6)+EDYEARS6+FEMALE6+BLACK6+HISP6+AGE6,data=dta21)
summary(lout1)

lout2 <- lm(I(ACASCALE13-ACASCALE11) ~ BECAMEUNINSURED13+BECAMEINSURED13+I(SELFINSURE13-SELFINSURE11)+
             I(MEDICAID13-MEDICAID11)+
             as.factor(PID11)+as.factor(ACASCALE6)+EDYEARS6+FEMALE6+BLACK6+HISP6,data=dta21)
summary(lout2)

texreg(
    list(lout1,lout2),digits=3,stars=0.05,
    file=paste0(
        the_prefix, "/figs/",
        "table2_tableA11_panel_uninsured_did.tex"
    )
    )



dta21$DH8_10T <- as.character(dta21$DH8_10)

dta21$DH8_10T[dta21$DH7_10=="Not covered by health insurance"] <- "Uninsured"
dta21$DH8_10T <- as.character(dta21$DH8_10T)

dta21$DH8_13T <- as.character(dta21$DH8)
dta21$DH8_13T[dta21$DH7=="Not covered by health insurance"] <- "Uninsured"
dta21$DH8_13T <- as.character(dta21$DH8_13T)

acarepeal_uninsured_11_13 <- (
  lm(
    ## acascale and acafavor are different
    ACASCALE13 ~ as.factor(DH8_10T)+as.factor(DH8_13T)+
      scale(ACASCALE6) + scale(ACASCALE7) + scale(ACASCALE11)+scale(PID6)+
      scale(EDYEARS6)+WHITE6+BLACK6+scale(INCOME1)+FEMALE6+scale(AGE6)+UNION0+CATHOLIC0+PROTESTANT0,
    data=dta21,
    weights=weight1_11
  )
)

stargazer(
    acarepeal_uninsured_11_13,
    digits=2,
    star.cutoffs = c(0.05, 0.01, 0.001),
    omit.stat=c("ser","f","adj.rsq", "rsq"),
    table.layout = "ts", float=F,
    out=paste0(
        the_prefix, "/figs/",
        "tableA12_uninsured_fall2018_aca_attitudes.tex"
    )
)

acarepeal_uninsured_13a <- (
  lm(
    ## acascale and acafavor are different
    ACASCALE13 ~ as.factor(DH8_10T)+
      scale(ACASCALE6) + scale(ACASCALE7) + scale(ACASCALE11)+scale(PID6),
     data=dta21,
    weights=weight1_11
  )
)


acarepeal_uninsured_13b <- (
  lm(
    ## acascale and acafavor are different
    ACASCALE13 ~ as.factor(DH8_10T)+
      scale(ACASCALE6) + scale(ACASCALE7) + scale(ACASCALE11)+scale(PID6)+
      scale(EDYEARS6)+WHITE6+BLACK6+scale(INCOME1)+FEMALE6+scale(AGE6)+UNION0+CATHOLIC0+PROTESTANT0,
    data=dta21,
    weights=weight1_11
  )
)

stargazer(
    acarepeal_uninsured_13a,
    acarepeal_uninsured_13b,
    digits=2,
    star.cutoffs = c(0.05, 0.01, 0.001),
    omit.stat=c("ser","f","adj.rsq", "rsq"),
    table.layout = "ts", float=F,
    out=paste0(
        the_prefix, "/figs/",
        "tableA13_fall2018_insurance_sttaus_aca_attitudes_extra_models.tex"
    )
)

pdf(
    paste0(
        the_prefix, "/figs/",
        "figureA6_panel-uninsured-09052019.pdf"
    ),
    width=4.5, height=4.5
)

par(mar=c(0, 5, 0, 1))
x2 <- rnorm(10000,mean=1.23,sd=0.43)
x1 <- rnorm(10000,mean=-0.03,sd=0.40)

par(pty="s")
plot(1,1,type="n",ylim=c(-2.25,2.25),xlim=c(0,1),
     xaxt="n",
     ylab="Coefficient on ACA REPEAL Attitudes",cex.lab=1.4,
     xlab="", bty="n", cex.axis=1.3
     )
point1 <- 0.25
point2 <- 0.75
points(y=mean(x1),x=point1,pch=16, cex=1.5)
points(y=mean(x2),x=point2,pch=17, cex=1.5)

lines(y=c(quantile(x1,.025),quantile(x1,.975)),x=c(point1,point1), lwd=2)
lines(y=c(quantile(x1,.05),quantile(x1,.95)),x=c(point1,point1),lwd=4)

lines(y=c(quantile(x2,.025),quantile(x2,.975)),x=c(point2,point2), lwd=2)
lines(y=c(quantile(x2,.05),quantile(x2,.95)),x=c(point2,point2),lwd=4)

abline(h=0,lty=2)
text(x=point1,y=-1.25,"Uninsured \n 2016", cex=1.3)
text(x=point2,y=-1.25,"Uninsured \n 2018", cex=1.3)

dev.off()
